<%--
  Created by IntelliJ IDEA.
  User: hongfive
  Date: 2019/1/7
  Time: 21:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basepath = request.getScheme()
            + "://" + request.getServerName()
            + ":" + request.getServerPort() + "/" + path;
%>
<html>
<head>
    <meta charset="UTF-8">
    <title>新建</title>
    <link rel="stylesheet" href="<%=basepath%>/statics/backcss/css/add.css">
    <link rel="stylesheet" href="<%=basepath%>/statics/backcss/css/bootstrap.min.css">
    <script src="<%=basepath%>/statics/backcss/js/jquery.min.js"></script>
    <script>
        function add(){
            var canvas = new Object();
            canvas.name=$("#name").val();
            canvas.creator=$("#creator").val();
            canvas.cgid=$("#cgids").val();
            canvas.price=$("#price").val();
            canvas.img=$("#img").val();
            canvas.description=$("#description").val();
            var canvasData = JSON.stringify(canvas);
            console.log(canvas);
            $.ajax({

                type:"Post",
                url:"../canvas/insertC",
                async:false,
                data:canvasData,
                contentType:'application/json;charset=utf-8',
                success:function (data) {
                    console.log(data);
                    var json=data;
                    if (json.code==1){
                        alter(json.message);
                        location.href="../category/index"
                    }
                    else {
                        alert("insertC success");
                    }
                }
            })
        }
        $(function(){
            var re_data = new Object();
            //添加关于分类名的关系  便于之后扩展
            $.ajax({
                url:'../category/Cglist',
                dataType: 'json',
                async:false,
                type:"GET",
                contentType:'application/json;charset=utf-8',
                success: function(data){
                    var json = data;
                    console.log(json);
                    if (json.code == 1){
                        console.log(json.data);
                        re_data = json.data;
                        //展示分类选项
                        for (var i=0;i<re_data.length;i++){
                            var dom="<option id=\"cgid\" value=\""+re_data[i].cgid+"\">"+re_data[i].cgName+"</option>"
                            $("#cgids").append(dom);

                        }
                    }
                    else {
                        re_data=json.message;
                    }
                },
                error: function (er) {
                    alert(er)
                }
            })
        })
    </script>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container">
        <div class="navbar-header">
            <a class="navbar-brand" href="#">
                油画管理
            </a>
        </div>
    </div>
</nav>
<div class="container">
    <div class="jumbotron">
        <h1>Hello,admin!</h1>
        <p>请小心的新增油画记录，要是建了一个错误的就不好了。。。</p>
    </div>
    <div class="page-header">
        <h3><small>新建</small></h3>
    </div>
    <form class="form-horizontal" action="../canvas/index" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">名称 ：</label>
            <div class="col-sm-8">
                <input name="name" class="form-control" id="name">
            </div>
        </div>
        <div class="form-group">
            <label for="creator" class="col-sm-2 control-label">画家名 ：</label>
            <div class="col-sm-8">
                <input name="creator" class="form-control" id="creator">
            </div>
        </div>
        <div class="form-group">
            <label for="cgids" class="col-sm-2 control-label">分类 ：</label>
            <%--JS导入分类名和分类id--%>
            <select id="cgids" name="cgid" class="col-sm-8 form-control" style="width: auto">
            </select>
        </div>
        <div class="form-group">
            <label for="price" class="col-sm-2 control-label">价格 ：</label>
            <div class="col-sm-8">
                <input name="price" type="number" class="form-control" id="price">
            </div>
        </div>
        <div class="form-group">
            <label for="img" class="col-sm-2 control-label">图片 ：</label>
            <div class="col-sm-8">
                <input id="img" name="img" class="file-loading"
                       type="file" multiple accept=".jpg,.jpeg,.png" data-min-file-count="1"
                       data-show-preview="true">
            </div>
        </div>
        <div class="form-group">
            <label for="description" class="col-sm-2 control-label">描述 ：</label>
            <div class="col-sm-8">
                <input name="description" type="text" class="form-control" id="description">
            </div>
        </div>


        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-primary" onclick="add()">保存</button>&nbsp;&nbsp;&nbsp;
            </div>
        </div>
    </form>
</div>
<footer class="text-center" >
</footer>
</body>
</html>

